package lzzy;

public class GenericSort {
    public static void main(String[] args) {
        Integer[] intArray = {2,4,3};
        Double[] doubleArray = {3.4, 1.3, -22.1};
        Character[] charArray = {'a','J','r'};
        String[] stringArray = {"Tom","Susan","Kim","yyy"};
        StudentScore[] studentArray = {new StudentScore("颜林廷","20200317081",100),
                                       new StudentScore("yyy","123456",85),
                                       new StudentScore("ttt","852963",90)};

        sort(intArray);
        sort(doubleArray);
        sort(charArray);
        sort(stringArray);
        // 新增排序学生类
        sort(studentArray);

        System.out.println("Sorted Integer objects：");
        printList(intArray);
        System.out.println("Sorted Double objects：");
        printList(doubleArray);
        System.out.println("Sorted Character objects：");
        printList(charArray);
        System.out.println("Sorted String objects：");
        printList(stringArray);
        // 新增学生成绩类，并升序显示
        System.out.println("学生排名：");
        printList(studentArray);

    }


    public static <E extends Comparable<E>> void sort(E[] list) {
        E currentMin;
        int currentMinIndex;

        for (int i = 0; i < list.length-1; i++){
            // 在列表中找到最小值[i..list.length-1]
            currentMin = list[i];
            currentMinIndex = i;

            for (int j = i + 1; j < list.length; j++){
                if (currentMin.compareTo(list[j]) > 0){
                    currentMin = list[j];
                    currentMinIndex = j;
                }
            }

            // 交换list[i]和list[currentMinIndex]如果需要的话
            if (currentMinIndex != i){
                list[currentMinIndex] = list[i];
                list[i] = currentMin;
            }
        }
    }

    public static void printList(Object[] list) {
        for (int i = 0; i< list.length; i++){
            System.out.println(list[i] + " ");
        }
        System.out.println();
    }
}
